package com.hs.lin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hs.ding.pojo.Dispatch;
import com.hs.ding.pojo.Need;
import com.hs.ding.pojo.Transport;
import com.hs.ding.pojo.Vehicle;
import com.hs.ding.service.IDispatchService;
import com.hs.ding.service.INeedService;
import com.hs.ding.service.ITransportService;
import com.hs.ding.service.IVehicleService;
import com.hs.dto.Constants;
import com.hs.dto.PageBean;
import com.hs.dto.ResultBean;
import com.hs.hu.pojo.Receipt;
import com.hs.hu.service.IReceiptService;
import com.hs.li.pojo.CarManagement;
import com.hs.li.pojo.CarType;
import com.hs.li.service.ICarManagementService;
import com.hs.li.service.ICarTypeService;
import com.hs.lin.dto.DispatchBean;
import com.hs.lin.service.IExceptionRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lin
 * @date 2022-06-20
 * @desc
 */
@RestController
@CrossOrigin
@RequestMapping("/track")
public class TrackController {
    @Autowired
    private IDispatchService dispatchService;

    @Autowired
    private ICarManagementService carManagementService;

    @Autowired
    private ICarTypeService carTypeService;

    @Autowired
    private IExceptionRegistrationService exceptionRegistrationService;

    //查询所有、分页展示、条件搜索

    /**
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResultBean findAllByPage(@RequestParam Integer currentPage, @RequestParam Integer pageSize){
        //查询所有派车单信息
        List<Dispatch> dispatchs = dispatchService.findDealDispatch();
        ArrayList<DispatchBean> list = new ArrayList<>();

        for (Dispatch dispatch : dispatchs) {
            DispatchBean dispatchBean = new DispatchBean();
            dispatchBean.setDispatchId(dispatch.getdOrderId());
            dispatchBean.setDispatchRoute(dispatch.getTransport().getNeed().getRoute());
            dispatchBean.setDispatchAppointTime(new Date());

            QueryWrapper<CarManagement> wrapper = new QueryWrapper<>();
            wrapper.eq("car_plate",dispatch.getTransport().getCarPlate());
            CarManagement carManagement = carManagementService.getOne(wrapper);
            QueryWrapper<CarType> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("id",carManagement.getTypeId());
            CarType carType = carTypeService.getOne(wrapper1);
            dispatchBean.setDispatchCarType(carType.getTypeName());

            dispatchBean.setDispatchCarCarrier(dispatch.getTransport().getCarrierName());
            dispatchBean.setDispatchCarPlate(dispatch.getTransport().getCarPlate());
            dispatchBean.setDispatchCarDriver(dispatch.getTransport().getDriverName());

//            QueryWrapper<ExceptionRegistration> wrapper2 = new QueryWrapper<>();
//            wrapper2.eq("c_id",dispatch.getdOrderId());
//            ExceptionRegistration exceptionRegistration = exceptionRegistrationService.getOne(wrapper2);
//            System.out.println("exceptionRegistration = " + exceptionRegistration);
            // 数据不全 报 空指针
//            dispatchBean.setNode(exceptionRegistration.getNode());

            dispatchBean.setDispatchOrderStatus(dispatch.geteStatus());
            list.add(dispatchBean);
        }
        PageBean<DispatchBean> pageBean = new PageBean();
        pageBean.setTotal(list.size());
        pageBean.setList(list);
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(Constants.SUCCESS_CODE);
        resultBean.setMsg("ok");
        resultBean.setData(pageBean);

        return resultBean;
    }

    @Autowired
    private IReceiptService receiptService;

    @Autowired
    private ITransportService transportService;

    @Autowired
    private IVehicleService vehicleService;

    @Autowired
    private INeedService needService;
    /**
     * 根据派车单号查询，弹窗信息回显
     * @return
     */
    @GetMapping("/{id}")
    public ResultBean queryById(@PathVariable String id){
        //根据派车单查询
        QueryWrapper<Dispatch> wrapper = new QueryWrapper<>();
        wrapper.eq("d_order_id",id);
        Dispatch dispatch = dispatchService.getOne(wrapper);

        QueryWrapper<Transport> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("t_order_id",dispatch.gettOrderId());
        Transport transport = transportService.getOne(wrapper1);

        QueryWrapper<Vehicle> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("v_order_id",transport.getvOrderId());
        Vehicle vehicle = vehicleService.getOne(wrapper2);

        QueryWrapper<Need> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("order_id",vehicle.getOrderId());
        Need need = needService.getOne(wrapper3);
        System.out.println("need = " + need);

        DispatchBean dispatchBean = new DispatchBean();
        dispatchBean.setDispatchId(id);

        dispatchBean.setDispatchOrderId(need.getOrderId());
        dispatchBean.setDispatchRoute(need.getRoute());
        dispatchBean.setDispatchAppointTime(new Date());

        QueryWrapper<CarManagement> wrapper4 = new QueryWrapper<>();
        wrapper4.eq("car_plate",transport.getCarPlate());
        CarManagement carManagement = carManagementService.getOne(wrapper4);
        QueryWrapper<CarType> wrapper5 = new QueryWrapper<>();
        wrapper5.eq("id",carManagement.getTypeId());
        CarType carType = carTypeService.getOne(wrapper5);
        dispatchBean.setDispatchCarType(carType.getTypeName());

        dispatchBean.setDispatchCarCarrier(transport.getCarrierName());
        dispatchBean.setDispatchCarPlate(transport.getCarPlate());
        dispatchBean.setDispatchCarDriver(transport.getDriverName());

        QueryWrapper<Receipt> wrapper6 = new QueryWrapper<>();
        wrapper6.eq("r_order_id",need.getOrderId());
        Receipt receipt = receiptService.getOne(wrapper6);
        dispatchBean.setRTime(receipt.getrTime());

        List<DispatchBean> dispatchBeans = new ArrayList<>();
        dispatchBeans.add(dispatchBean);
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(Constants.SUCCESS_CODE);
        resultBean.setMsg("okk");
        resultBean.setData(dispatchBeans);
        return resultBean;
    }
}
